import dayjs from 'dayjs'
import { isEmpty, isArray, cloneDeep, concat, isNumber, clone } from 'lodash-es'
import {
  formatDate
} from '@/sheep/helper/formatTime'
import { getDictOptions, getDictLabel } from '@/sheep/helper/dict'

export const getDefaultValue = (value, fieldType) => {
  const userInfo = uni.getStorageSync('userInfo')
  if (['String', 'textarea'].includes(fieldType)) {
    // 当前用户角色编码
    if (value === 'currentUserCode') {
      return userInfo?.user?.userCode
    }
    // 当前用户部门编码
    if (value === 'currentDeptCode') {
      return userInfo?.user?.deptCode
    }
    return value
  }

  if (['dic_radio', 'seachwindow'].includes(fieldType)) {
    return value
  }

  if (fieldType === 'LocalDateTime') {
    // 当前日期时间
    if (value === 'currentLocalDateTime') {
      return dayjs().valueOf()
    } else {
      if (isNumber(Number(value))) {
        return Number(value)
      } else {
        return null
      }
    }
  }
	
	

  const numTypeList = ['Long', 'Integer', 'Double', 'BigDecimal']
  if (numTypeList.includes(fieldType)) {
    if (isNumber(Number(value))) {
      return Number(value)
    } else {
      return null
    }
  }
	
	if (fieldType === 'dic_multpie') {
	  if (value) {
	    return value.split(',')
	  }
	  return value
	}

  if (['ImageFile','DocumentFile','VideoFile'].includes(fieldType)) {
    if (value) {
      return value.split(',')
    }
    return []
  }

  if (fieldType === 'Boolean') {
    if (value === 'true') {
      return true
    } else if (value === 'false') {
      return false
    } else {
      return null
    }
  }
  return value
}

export const getFormat = (fieldType) => {
  if (fieldType === 'Boolean') {
    return 'boolean'
  }
  const numTypeList = ['Long', 'Integer', 'Double', 'BigDecimal', 'LocalDateTime']
  if (numTypeList.includes(fieldType)) {
	 return 'number'
  }
  if (['dic_multpie','ImageFile','DocumentFile','VideoFile'].includes(fieldType)) {
    return 'array'
  }
  if (['String', 'textarea', 'dic_radio'].includes(fieldType)) {
	 return 'string'
  }
  return 'string'
}

export const handleReturnValue = ({ fieldValue, fieldType, dictType }) => {
  if (fieldType === 'LocalDateTime') {
    return formatDate(fieldValue)
  }
  if (['Long', 'Integer', 'Double', 'String', 'textarea'].includes(fieldType)) {
    return fieldValue
  }
  if (fieldType === 'Boolean') {
    if (fieldValue === true) {
      return '是'
    }
    if (fieldValue === false) {
      return '否'
    }
	   return '-'
  }
	
	if (['ImageFile','DocumentFile','VideoFile'].includes(fieldType)) {
	  return fieldValue || []
	}
	
  if (fieldType === 'dic_radio') {
	  return getDictLabel(dictType, fieldValue)
  }
  if (fieldType === 'dic_multpie') {
    const dictOptions = getDictOptions(dictType) || []
    const _fieldValue = fieldValue || []
    const textArr = []
    _fieldValue.forEach(el => {
      const data = dictOptions.find((dict) => dict.value === el)
      if (data) {
        textArr.push(data.label)
      }
    })
		console.log('textArr----',textArr);
	  return textArr.join(',')
  }
}
